<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 天气显示 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-cascader :options="city" placeholder="请选择城市" :show-all-levels="false" v-model="selectedCity"></el-cascader>
            <el-button type="primary" icon="el-icon-search" style="margin-left: 10px" @click="handleSearch">查看</el-button>
            <el-button type="danger" icon="el-icon-s-tools" @click="subscribe">预约</el-button>
            <el-table
                v-loading="isShowLoading"
                element-loading-text="努力加载七天天气..."
                :data="tableData"
                border
                style="margin-top: 20px"
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column prop="date" label="日期"></el-table-column>
                <el-table-column prop="day" label="天数"></el-table-column>
                <el-table-column prop="weather" label="天气"></el-table-column>
                <el-table-column prop="lowTemp" label="低温"></el-table-column>
                <el-table-column prop="highTemp" label="高温"></el-table-column>
                <el-table-column prop="windDirection1" label="风向"></el-table-column>
                <el-table-column prop="windSpeed" label="风速"></el-table-column>
            </el-table>
            <div id="main" style="width: 100%; height: 400px; margin-top: 20px" ref="main"></div>
        </div>
        <el-dialog title="预约地点和日期" :visible.sync="dialogFormVisible">
            <el-form :model="form">
                <el-form-item label="选择要去的城市" :label-width="formLabelWidth">
                    <el-cascader
                        ref="city"
                        :options="city1"
                        placeholder="请选择城市"
                        :show-all-levels="false"
                        v-model="form.selectedCity"
                    ></el-cascader>
                </el-form-item>
                <el-form-item label="选择日期" :label-width="formLabelWidth">
                    <el-date-picker
                        v-model="form.date"
                        type="daterange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                    >
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>


<script>
import { getCityData, getWeatherData, subscribeWeather } from '../../api/index';
import echarts from 'echarts';
import moment from 'moment';
export default {
    data() {
        return {
            city: [],
            city1: [],
            selectedCity: '',
            tableData: [],
            isShowLoading: false,
            dialogFormVisible: false,
            formLabelWidth: '120px',
            form: {
                selectedCity: '',
                date: ''
            }
        };
    },
    created() {
        this.getCityList();
    },
    mounted() {
        let that = this;
        this.isShowLoading = true;
        this.selectedCity = '101010100';
        getWeatherData({ cityCode: '101010100' }).then((res) => {
            that.tableData = res.data;
            that.isShowLoading = false;
            let dateArr = [];
            let minTemps = [];
            let maxTemps = [];
            for (let i = 0; i < that.tableData.length; i++) {
                dateArr.push(that.tableData[i].date);
                if (that.tableData[i].lowTemp.indexOf('-') != -1) {
                    minTemps.push(that.tableData[i].lowTemp.substring(0, 2));
                } else {
                    minTemps.push(that.tableData[i].lowTemp.substring(0, 1));
                }

                if (that.tableData[i].highTemp.indexOf('-') != -1) {
                    maxTemps.push(that.tableData[i].highTemp.substring(0, 2));
                } else {
                    maxTemps.push(that.tableData[i].highTemp.substring(0, 1));
                }
            }
            that.getWeatherData(dateArr, minTemps, maxTemps);
        });
    },
    methods: {
        handleSearch() {
            let city = '';
            if (this.selectedCity.length == 2) {
                city = this.selectedCity[1];
            } else if (this.selectedCity.length == 3) {
                city = this.selectedCity[2];
            }
            let that = this;
            let params = {
                cityCode: city
            };
            this.isShowLoading = true;
            getWeatherData(params).then((res) => {
                that.tableData = res.data;
                that.isShowLoading = false;
                let dateArr = [];
                let minTemps = [];
                let maxTemps = [];
                for (let i = 0; i < that.tableData.length; i++) {
                    dateArr.push(that.tableData[i].date);
                    if (that.tableData[i].lowTemp.indexOf('-') != -1) {
                        minTemps.push(that.tableData[i].lowTemp.substring(0, 2));
                    } else {
                        minTemps.push(that.tableData[i].lowTemp.substring(0, 1));
                    }
                    if (that.tableData[i].highTemp.indexOf('-') != -1) {
                        maxTemps.push(that.tableData[i].highTemp.substring(0, 2));
                    } else {
                        maxTemps.push(that.tableData[i].highTemp.substring(0, 1));
                    }
                }
                that.getWeatherData(dateArr, minTemps, maxTemps);
            });
        },
        getCityList() {
            getCityData().then((res) => {
                this.city = res;
                this.city1 = res;
            });
        },
        subscribe() {
            this.dialogFormVisible = true;
        },
        submit() {
            let startDate = moment(this.form.date[0]).format('YYYY/MM/DD');
            let endDate = moment(this.form.date[1]).format('YYYY/MM/DD');
            let date = startDate + '-' + endDate;

            // let city;
            // if (this.form.selectedCity.length == 2) {
            //     city = this.form.selectedCity[1];
            // } else if (this.form.selectedCity.length == 3) {
            //     city = this.form.selectedCity[2];
            // }

            console.log(this.$refs['city']);
            console.log(this.$refs['city'].getCheckedNodes()[0].pathLabels);
            let path = this.$refs['city'].getCheckedNodes()[0].pathLabels.length;
            let city = this.$refs['city'].getCheckedNodes()[0].pathLabels[path - 1];
            let form = {
                selectedCity: city,
                date: date
            };
            console.log(form);
            let that = this;
            this.$notify.info({
                title: '消息',
                message: '发送邮件中。。。'
            });
            subscribeWeather(form).then((result) => {
                console.log(result);
                that.dialogFormVisible = false;

                that.$notify({
                    title: '成功',
                    message: '邮件发送成功!',
                    type: 'success'
                });
            });
        },
        getWeatherData(dateArr, minTemps, maxTemps) {
            let mycharts = echarts.init(document.getElementById('main'));
            var option = {
                // backgroundColor: 'rgba(252,173,54,0.1)',
                //图表标题
                title: {
                    left: 20,
                    top: 10, //进行标题位置的微调，top:10 距离顶端10px
                    x: 'left', //标题的水平位置；left-左（默认）；right-右
                    y: 'top', //垂直位置：bottom-将标题置于表底；center-中间；top-上
                    text: '未来一周气温'
                },
                legend: {
                },
                //提示框
                tooltip: {
                    backgroundColor: 'cornflowerblue' //设置背景色
                },
                //工具箱
                toolbox: {
                    right: 25,
                    top: 10,
                    show: true,
                    //结构-样式图
                    feature: {
                        //mark是辅助线开关
                        mark: {
                            //                            show:true
                        },
                        //数据可视化标签
                        dataView: {
                            //                            show:true,
                            readOnly: false //可修改数据
                        },
                        saveAsImage: {}, //下载标签
                        magicType: { type: ['line', 'bar'] } //可更换图表样式标签
                    }
                },
                //视觉映射组件
                visualMap: {
                    right: 2,
                    bottom: 10,
                    //有几个花括号就代表分成几个区域显示
                    pieces: [
                        {
                            gt: -10,
                            lte: 0,
                            color: '#096'
                        },
                        {
                            gt: 0,
                            lte: 10,
                            color: '#ffde33'
                        },
                        {
                            gt: 10,
                            lte: 20,
                            color: '#ff9933'
                        }
                    ]
                },
                grid: { x: 50, y: 50, x2: 100, y2: 50 },
                xAxis: [
                    {
                        data: dateArr
                    }
                ],
                yAxis: {},
                //图标核心内容
                series: [
                    {
                        name: '最高气温',
                        type: 'line',
                        color: 'green',
                        markLine: { data: [{ type: 'average', name: '平均值' }] },
                        markPoint: {
                            data: [
                                { type: 'max', name: '最大值' },
                                { type: 'min', name: '最小值' }
                            ]
                        },
                        data: maxTemps
                    },
                    {
                        name: '最低气温',
                        type: 'line',
                        color: 'blue',
                        markLine: { data: [{ type: 'average', name: '平均值' }] },
                        markPoint: {
                            data: [
                                { type: 'max', name: '最大值' },
                                { type: 'min', name: '最小值' }
                            ]
                        },
                        data: minTemps
                    }
                ]
            };
            mycharts.setOption(option);
        }
    }
};
</script>